package com.atguigu.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


@Configuration
@EnableWebSecurity //是开启SpringSecurity的默认行为
@EnableGlobalMethodSecurity(prePostEnabled = true)  //启用权限的验证
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    /*@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("lucky")
                .password(new BCryptPasswordEncoder().encode("123456"))
                .roles("admin","manager");
    }*/


    /**
     *    todo：让iframe可以正常显示
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //允许iframe嵌套显示  允许相同域名
        http.headers().frameOptions().sameOrigin();
        //自定义登录功能不采用默认策略
        //让静态资源不经过认证就可以直接访问
        http.authorizeRequests()
                .antMatchers("/static/**","/loginPage").permitAll()    //不用认证授权可以直接访问
                //.antMatchers("").hasRole("")  //具有指定的角色才可以访问，先认证通过
                //.antMatchers("").hasAuthority("") //具有指定的权限才可以访问，先认证通过
                //.antMatchers("").hasAnyRole("") //具有指定的多个角色中一个就可以访问，先认证通过
                //.antMatchers("").hasAnyAuthority("") //具有指定的多个权限中一个就可以访问，先认证通过
                .anyRequest().authenticated() //只要认证通过即可访问
                .and()
                .formLogin()
                .loginPage("/loginPage") //表单页面的地址
                //.usernameParameter("username") // 如果省略，必须是username
                //.passwordParameter("password") //如果省略，必须是password
                .loginProcessingUrl("/login")  //提交表单的处理地址
                //.failureForwardUrl("/login") //登录失败后哪里
                .defaultSuccessUrl("/") //登录成功后哪里
                .and()
                .logout().logoutUrl("/logout") //退出登录请求路径
                .invalidateHttpSession(true) //清除信息
                .logoutSuccessUrl("/loginPage") //退出登录成功的页面
                .and()
                .csrf()
                .disable();   //注销功能需要禁用csrf

        //添加自定义无权限处理器
        http.exceptionHandling().accessDeniedHandler(new MyAccessDeineHandler());

    }

    @Bean //创建bean  bean的id就是方法名passwordEncoder，并放到IOC容器中
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

}
